﻿using Common.AspNetCore.Structs;
using Common.EF;
using Common.Infrastructure; 
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.AspNetCore
{
    public class SysApiControllerBase : ApiController
    {
        private readonly ICommonModelRepository repository; 
        private readonly ICommonRepository commonRepository;
        private readonly IConfiguration configuration;

        public SysApiControllerBase(IServiceProvider provider)
            : base(provider)
        {
            repository = provider.GetService<ICommonModelRepository>();
            commonRepository = provider.GetService<ICommonRepository>();
            configuration = provider.GetService<IConfiguration>();
        }

        public ApiActionResult<object> DoGetIdModel(ModelIdRequest modelRequest)
        {
            return SafeExecute<object>((param) =>
            {
                return repository.GetModelByIdByReqest(modelRequest);
            });
        }

        public ApiActionResult<object> DoGetModel(ModelRequest modelRequest)
        {
            return SafeExecute<object>((param) =>
            {
                return repository.GetModelByRequest(modelRequest);
            });
        }

        public ApiActionResult<object> DoGetOrderModel(ModelRequest modelRequest)
        {
            return SafeExecute<object>((param) =>
            {
                return repository.GetOrderModelByRequest(modelRequest);
            });
        }

        public ApiActionResult<object> DoGetOrderPageModel(ModelOrderPageRequest modelRequest)
        {
            return SafeExecute<object>((param) =>
            {
                return repository.GetOrderPageModelByRequest(modelRequest);
            });
        }

        public ApiActionResult<List<string>> DoGetEntityIds(IDParam idModel)
        {
            return SafeExecute<List<string>>((param) =>
            {
                return repository.GetIdsByEntity(idModel);
            });
        }

        public async Task<ApiActionResult<bool>> DoSaveModel(ModelSaveRequest request)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                await repository.SaveModelsAsync(request);
                return true;
            });
        }

        public async Task<ApiActionResult<bool>> DoDeleteModel(ModelDeleteRequest request)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                await repository.DeleteModelsAsync(request);
                return true;
            });
        }

        public ApiActionResult<bool> DoDatabaseMigrate(DatabaseMigrateParam migrateParam)
        {
            return SafeExecute<bool>((param) =>
            { 
                var beginDay = LibSysUtils.ToInt(this.configuration["Migrate:Begin"]);
                var endDay = LibSysUtils.ToInt(this.configuration["Migrate:End"]);
                if (beginDay != 0 && migrateParam.BeginTime == null)
                {
                    migrateParam.BeginTime = DateTime.Now.Date.AddDays(-beginDay);
                }

                if (endDay != 0 && migrateParam.EndTime == null)
                {
                    migrateParam.EndTime = DateTime.Now.Date.AddDays(endDay);
                }

                if (migrateParam.BeginTime == null)
                {
                    migrateParam.BeginTime = DateTime.Now.AddMonths(-3);
                }

                if (migrateParam.EndTime == null)
                {
                    migrateParam.EndTime = DateTime.Now;
                }

                commonRepository.DatabaseMigrateAsync(migrateParam.BeginTime, migrateParam.EndTime).Wait();
                return true;
            });
        }
    }
}
